
package GUI.main.dialogs;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.AbstractListModel;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListModel;

import officeserver.log_error.AppointmentException;
import officeserver.log_error.UserException;
import officeserver.report.Appointment;
import officeserver.users.AccessLevel;

import GUI.helpers.ButtonType;
import GUI.helpers.PButton;
import GUI.helpers.ProjectConstants;
import GUI.main.MainClient;
import GUI.main.renderers.AppointmentListRenderer;

/**
 * @author mramsey3
 * 
 */
public class PatientAppointmentsDialog extends JDialog implements
	MouseListener, ActionListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel topPanel;
    private JPanel bottomPanel;

    private JScrollPane appointmentPane;
    private JList appointmentList;

    private PButton closeButton;

    public final static int WIDTH = 400;
    public final static int HEIGHT = 400;

    /**
     * 
     */
    public PatientAppointmentsDialog() {
	setSize(new Dimension(WIDTH, HEIGHT));

	setLayout(new BorderLayout());
	final Toolkit toolkit = Toolkit.getDefaultToolkit();
	final Dimension screenSize = toolkit.getScreenSize();
	final int x = (screenSize.width - WIDTH) / 2;
	final int y = (screenSize.height - HEIGHT) / 2;
	setLocation(x, y);
	add(getTopPanel(), BorderLayout.NORTH);
	add(getAppointmentPane(), BorderLayout.CENTER);
	add(getBottomPanel(), BorderLayout.SOUTH);
    }

    /**
     * @return
     */
    private JPanel getTopPanel() {
	if (topPanel == null) {
	    topPanel = new JPanel() {
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(ProjectConstants.SUBHEADER_COLOR);
		    g.fillRect(0, 0, getWidth(), getHeight());
		    g.setColor(Color.white);
		    g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 24));
		    g.drawString("appointments:", 24, getHeight() - 8);
		}
	    };
	    topPanel.setPreferredSize(new Dimension(WIDTH, ProjectConstants.HEADER_HEIGHT));
	}
	return topPanel;
    }

    /**
     * @return
     */
    private JScrollPane getAppointmentPane() {
	if (appointmentPane == null) {
	    appointmentPane = new JScrollPane();
	    appointmentPane.setPreferredSize(new Dimension(200, 0));
	    appointmentPane.setViewportView(getAppointmentList());
	}
	return appointmentPane;
    }

    /**
     * @return
     */
    private JList getAppointmentList() {
	if (appointmentList == null) {
	    appointmentList = new JList(new AppointmentListModel());
	    appointmentList.setCellRenderer(new AppointmentListRenderer());
	    appointmentList.addMouseListener(new DoubleClickListener(
		    appointmentList));
	}
	return appointmentList;
    }

    /**
     * @return
     */
    private JPanel getBottomPanel() {
	if (bottomPanel == null) {
	    bottomPanel = new JPanel();

	    bottomPanel.add(getCloseButton());
	}
	return bottomPanel;
    }



    /**
     * @return
     */
    private PButton getCloseButton() {
	if (closeButton == null) {
	    closeButton = new PButton("Close",ButtonType.MEDIUM);
	    closeButton.addActionListener(this);
	}
	return closeButton;
    }

    /**
     * 
     */
    public void refreshPanel() {
	appointmentList.updateUI();
	repaint();
    }

    /**
     * @author mramsey3
     * 
     */
    private class AppointmentListModel extends AbstractListModel {
	private static final long serialVersionUID = 1L;

	public Object getElementAt(int i) {
	    ArrayList<Appointment> appointments = new ArrayList<Appointment>();
	    try {
		appointments = MainClient.getConnection().getSelfAppointments();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
		catch (AppointmentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return appointments.get(i);
	}

	public int getSize() {
	    ArrayList<Appointment> appointments = new ArrayList<Appointment>();
	    try {
		appointments = MainClient.getConnection().getSelfAppointments();
	    } catch (UserException e) {
		e.printStackTrace();
	    }
		catch (AppointmentException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return appointments.size();
	}
    }

    /**
     * @author mramsey3
     * 
     */
    private class DoubleClickListener extends MouseAdapter {
	protected JList list;

	/**
	 * @param l
	 */
	public DoubleClickListener(JList l) {
	    list = l;
	}

	public void mouseClicked(MouseEvent e) {
	    if (e.getSource() == appointmentList)
		if (e.getClickCount() == 2) {
		    if (appointmentList.getModel().getSize() > 0) {
			int index = list.locationToIndex(e.getPoint());
			ListModel lm = list.getModel();
			Appointment a = (Appointment) (lm.getElementAt(index));
			ReportDialog report = new ReportDialog(a);
			report.setVisible(true);
			refreshPanel();
		    }
		}
	}
    }

    public void actionPerformed(ActionEvent e) {

	if (e.getSource() == getCloseButton()) {
	    this.setVisible(false);
	}
    }

    public void mouseClicked(MouseEvent arg0) {
    }
    public void mouseEntered(MouseEvent arg0) {
    }
    public void mouseExited(MouseEvent arg0) {
    }
    public void mousePressed(MouseEvent arg0) {
    }
    public void mouseReleased(MouseEvent arg0) {
    }

}
